//
//  InvestmentProjectController.m
//  DianDangDai
//
//  Created by bhf on 2017/12/13.
//  Copyright © 2017年 bhf. All rights reserved.
//

#import "InvestmentProjectController.h"
#import "InvestmentCell.h"
#import "InvestmentDetailViewController.h"
#import "InvestmentModel.h"
#import "HomePageDetailController.h"
@interface InvestmentProjectController ()
@property (nonatomic, assign) BOOL minormax;
@property (nonatomic, assign) BOOL minormax2;
@property (nonatomic, assign) BOOL minormax3;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) InvestmentModel *xsModel;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger currentType;
@end

@implementation InvestmentProjectController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    _minormax = YES;
    _minormax2 = YES;
    _minormax3 = YES;
    [self setupViews];
    [self requestDataWithorderType:1 showHUD:YES];
    _currentType = 1;
    self.title = @"我要出借";
    // Do any additional setup after loading the view.
}

- (void)requestDataWithorderType:(NSInteger)type showHUD:(BOOL)show {
    _page = 1;
    self.dataArray = [NSMutableArray array];
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1006&currPage=1&pageSize=10&orderType=%ld",API_LOGIN_URL_STRING,type];
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        for (NSDictionary *dic in dict[@"biaoList"]) {
            InvestmentModel *model = [InvestmentModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
        _xsModel = [InvestmentModel mj_objectWithKeyValues:dict[@"tianbiao"]];
        [self setupXSB];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    } showHUD:show];
}

- (void)setupXSB {
    
    self.apr.text = [NSString stringWithFormat:@"%.1f",_xsModel.apr];
    NSString *period;
    if (_xsModel.period_unit == 1) {
        period = @"天";
    } else if (_xsModel.period_unit == 0) {
        period = @"个月";
    } else if (_xsModel.period_unit == -1) {
        period = @"年";
    }
    self.timeLine.text = [NSString stringWithFormat:@"%ld%@",_xsModel.period,period];
    self.money.text = [NSString stringWithFormat:@"%.2f¥",_xsModel.symoney];
}

- (void)setupViews {
    
    _defult.layer.borderWidth = 0.5;
    _defult.layer.borderColor = [[UIColor colorWithHex:@"#ebebeb"] CGColor];
    _earning.layer.borderWidth = 0.5;
    _earning.layer.borderColor = [[UIColor colorWithHex:@"#ebebeb"] CGColor];
    _deadline.layer.borderWidth = 0.5;
    _deadline.layer.borderColor = [[UIColor colorWithHex:@"#ebebeb"] CGColor];
    _moneyAmount.layer.borderWidth = 0.5;
    _moneyAmount.layer.borderColor = [[UIColor colorWithHex:@"#ebebeb"] CGColor];
    _ljtz.layer.cornerRadius = _ljtz.frame.size.height/2;
    _ljtz.layer.borderColor = [[UIColor colorWithHex:@"#948cd2"] CGColor];
    _ljtz.layer.borderWidth = 1;
    self.navigationItem.backBarButtonItem = BarButtonItem;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithorderType:_currentType showHUD:NO];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}

- (void)loadMoreData {
    _page++;
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1006&currPage=%ld&pageSize=10&orderType=%ld",API_LOGIN_URL_STRING,_page,_currentType];
  
    [[CDHttpHelper defaultCDHttpHelper] get:urlString params:nil success:^(id responseObj) {
        NSDictionary *dict = [responseObj objectFromJSONData];
        NSLog(@"%@",dict);
        for (NSDictionary *dic in dict[@"biaoList"]) {
            InvestmentModel *model = [InvestmentModel mj_objectWithKeyValues:dic];
            [self.dataArray addObject:model];
        }
    
        [self.tableView.mj_footer endRefreshing];
        
        if (_dataArray.count%10 != 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer resetNoMoreData];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    } showHUD:NO];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   
    
    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *identifier;
    identifier = @"imt_cell";
    InvestmentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    InvestmentModel *model = _dataArray[indexPath.row];
    [cell setModel:model];
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    InvestmentModel *model = self.dataArray[indexPath.row];
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Investment" bundle:[NSBundle mainBundle]];
    InvestmentDetailViewController *itdvc = [board instantiateViewControllerWithIdentifier:@"Investment"];
    [itdvc.tabBarController.tabBar setHidden:YES];
    itdvc.hidesBottomBarWhenPushed = YES;
    itdvc.id = model.bidId;
    [self.navigationController pushViewController:itdvc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 145;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//切换排序方式
- (IBAction)changeModel:(UIButton *)sender {
   
    [self.tableView.mj_footer resetNoMoreData];
    sender.titleLabel.textColor = [UIColor blackColor];
    if (sender.tag == 101) {
        [self requestDataWithorderType:1 showHUD:YES];
        _minormax = YES;
        _minormax2 = YES;
        _minormax3 = YES;
        [sender setTitleColor:[UIColor colorWithHex:@"#118cf8"] forState:UIControlStateNormal];
        [_deadline setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_deadline setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_moneyAmount setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_moneyAmount setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_earning setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_earning setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    } else if (sender.tag == 102) {
        [sender setTitleColor:[UIColor colorWithHex:@"#118cf8"] forState:UIControlStateNormal];
        _minormax2 = YES;
        _minormax3 = YES;
        if (_minormax == YES) {
             [sender setImage:[UIImage imageNamed:@"icon_down2"] forState:UIControlStateNormal];
            [self requestDataWithorderType:3 showHUD:YES];
            _currentType = 3;
            _minormax = NO;
        } else {
            [sender setImage:[UIImage imageNamed:@"icon_down3"] forState:UIControlStateNormal];
            [self requestDataWithorderType:4 showHUD:YES];
            _currentType = 4;
            _minormax = YES;
        }
        
        [_deadline setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_deadline setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_moneyAmount setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_moneyAmount setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_defult setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    } else if (sender.tag == 103) {
        [sender setTitleColor:[UIColor colorWithHex:@"#118cf8"] forState:UIControlStateNormal];
        _minormax = YES;
        _minormax3 = YES;
        if (_minormax2 == YES) {
            [sender setImage:[UIImage imageNamed:@"icon_down2"] forState:UIControlStateNormal];
            [self requestDataWithorderType:7 showHUD:YES];
            _currentType = 7;
            _minormax2 = NO;
        } else {
            [sender setImage:[UIImage imageNamed:@"icon_down3"] forState:UIControlStateNormal];
            [self requestDataWithorderType:8 showHUD:YES];
            _currentType = 8;
            _minormax2 = YES;
        }
        
        [_moneyAmount setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_moneyAmount setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_earning setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_earning setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_defult setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    } else if (sender.tag == 104) {
        [sender setTitleColor:[UIColor colorWithHex:@"#118cf8"] forState:UIControlStateNormal];
        _minormax2 = YES;
        _minormax = YES;
        if (_minormax3 == YES) {
            [sender setImage:[UIImage imageNamed:@"icon_down2"] forState:UIControlStateNormal];
            [self requestDataWithorderType:1 showHUD:YES];
            _currentType = 1;
            _minormax3 = NO;
        } else {
            [sender setImage:[UIImage imageNamed:@"icon_down3"] forState:UIControlStateNormal];
            [self requestDataWithorderType:2 showHUD:YES];
            _currentType = 2;
            _minormax3 = YES;
        }
        
        [_deadline setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_deadline setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_earning setImage:[UIImage imageNamed:@"icon_down-1"] forState:UIControlStateNormal];
        [_earning setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_defult setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
}
- (IBAction)touzi:(UIButton *)sender {
//    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Investment" bundle:[NSBundle mainBundle]];
//    InvestmentDetailViewController *itdvc = [board instantiateViewControllerWithIdentifier:@"Investment"];
//    [itdvc.tabBarController.tabBar setHidden:YES];
//    itdvc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:itdvc animated:YES];
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"HomePage" bundle:[NSBundle mainBundle]];
    HomePageDetailController *hpdc = [board instantiateViewControllerWithIdentifier:@"hpdc"];
    hpdc.hidesBottomBarWhenPushed = YES;
    hpdc.titleStr = @"开通资金托管";
    NSString *urlString = [NSString stringWithFormat:@"%@OPT=1038&sign=%@&equipment=2",API_LOGIN_URL_STRING,USER_SIGN];
    hpdc.filepath = [NSURL URLWithString:urlString];
    [self showViewController:hpdc sender:self];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
